<?php
/*
 * wap 2.0 注册模块
 */

if (!defined('IN_SEXMALL')){
	die('Hacking attempt');
}

// 检查登录
if ($user_token){
	wap_header("Location: ./");
}

$step = empty($_REQUEST['step']) ? 'register' : trim($_REQUEST['step']);

$username = isset($_GET['username']) ? ($_GET['username']) : '';

/* 注册页面 */
if ($step == 'register')
{
	/* 验证码相关设置 */
	if ((intval($_CFG['captcha']) & CAPTCHA_REGISTER) && gd_version() > 0)
	{
		$smarty->assign('enabled_captcha', 1);
		$smarty->assign('rand',            mt_rand());
	}

	// 移动支付 活动
	$credtential = wap_getcookie("WAP[credtential]");
	if (!empty($credtential) && empty($username)){
		$username = $credtential;
	}

	/* OMNITURE 统计  */
	include 'omniture/user_register.php';

	$smarty->assign('username', 		$username);
	$smarty->assign('disable_search', 	1);
	$smarty->assign('ur_here',    		assign_ur_here('注册'));

	$smarty->display('user_register.dwt');
}

/* 提交注册 */
else if ($step == 'act_register')
{
	$now = local_time();
	$data = isset($_POST['data']) ? trim($_POST['data']) : '';

	list($username, $timestamp, $password, $password2) = explode(':', $data);

	$timestamp = base64_decode($timestamp);
	$username = base64_decode($username);
	$password = base64_decode($password);
	$password2 = base64_decode($password2);

	if ($now - $timestamp >= 600){
		$error['error'] = 'error';
		$error['title'] = '注册失败：操作超时';
		$error['act'] 	= '重新注册';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
		show_message($error['title'], $error['act'], $error['url'], $error['error']);
	}

	$gender   = isset($_POST['sex']) ? intval($_POST['sex']) : '0';

	if ($gender){
		$gender = '1';
	}else{
		$gender = '0';
	}

	// 设置跳转页面
	$back_act = wap_getcookie("WAP[back_act]");
	wap_setcookie("WAP[back_act]");
	if (empty($back_act) && isset($_POST['back_act']))
	{
		$back_act = trim($_POST['back_act']);
	}
	else if (empty($back_act))
	{
		$back_act = 'user.html';
	}

	$error = array();
	if (empty($username))
	{
		$error['error'] = 'error';
		$error['title'] = '请输入用户名';
		$error['act'] 	= '返回上一页';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;

	}else if (!preg_match("/^1[3458][0-9]{9}$/", $username)
	&& !preg_match("/^[\w\d]+[\w\d-.]*@[\w\d-.]+\.[\w\d]{2,10}$/i", $username)){
		$error['error'] = 'error';
		$error['title'] = '用户名格式错误：用户名为邮箱地址或手机号码';
		$error['act'] 	= '返回上一页';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
	}
	else if (empty($password))
	{
		$error['error'] = 'error';
		$error['title'] = '请输入密码';
		$error['act'] 	= '返回上一页';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
	}
	else if (is_numeric($password) || ctype_alpha($password))
	{
		$error['error'] = 'error';
		$error['title'] = '请输入6-20位数字字母的组合';
		$error['act'] 	= '返回上一页';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
	}
	else if ($password != $password2)
	{
		$error['error'] = 'error';
		$error['title'] = '再次输入密码不一样';
		$error['act'] 	= '返回上一页';
		$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
	}
	else if ((intval($_CFG['captcha']) & CAPTCHA_REGISTER) && gd_version() > 0)
	{
		if (empty($_POST['captcha']))
		{
			$error['error'] = 'error';
			$error['title'] = '请输入验证码';
			$error['act'] 	= '返回上一页';
			$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
		}
		else
		{
			/* 检查验证码 */
			include_once('includes/cls_captcha.php');
			$validator = new captcha();
			if (!$validator->check_word($_POST['captcha']))
			{
				$error['error'] = 'error';
				$error['title'] = '验证码错误';
				$error['act'] 	= '返回上一页';
				$error['url'] 	= 'index.php?m=user&act=register&username='.$username;
			}
		}
	}

	if (!empty($error))
	{
		show_message($error['title'], $error['act'], $error['url'], $error['error']);
	}

	$result = $mInterface->addUserBase($username, $password, $gender);

	//var_dump($result);exit;
	if (is_array($result) && $result['status'] && is_array($result['result']))
	{
		$user = $result['result'];

		// 设置COOKIE
		$str = substr(md5(AUTH_KEY), -8);
		$auth_str = encrypt($str, AUTH_KEY);
		$auth = encrypt($username, AUTH_KEY).':'.$auth_str;

		wap_setcookie("WAP[auth]", $auth, -1);
		wap_setcookie("WAP[auth_token]", encrypt($user['user_token'], AUTH_KEY), -1);

		MInterface::$login_id = $username;
		MInterface::$user_token = $user['user_token'];

		// 设置 memcache
		set_session($username, $user['user_token']);

		$title = wap_getcookie('back_act_title') ? wap_getcookie('back_act_title') : '返回注册前页面';
		show_message('注册成功'.$username, array($title, '用户中心'), array(urldecode($back_act), 'user.html'), 'info', false);
	}
	else if(!$result['status'] && $result['msg'])
	{
		show_message($result['msg'], '返回上一页', 'index.php?m=user&act=register');
	}
	else{
		show_message('注册失败', '返回上一页', 'index.php?m=user&act=register');
	}
}
